﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace socialnexus.classes.models
{
    public class AdminUser
    {
        // Members
        public static string TableName = "AdminUsers";

        private int _AdminUserId;
        public int AdminUserId
        {
            get { return _AdminUserId; }
            set { _AdminUserId = value; }
        }

        private string _Username;
        public string Username
        {
            get { return _Username; }
            set { _Username = value; }
        }

        private string _Password;
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        private string _FirstName;
        public string FirstName
        {
            get { return _FirstName; }
            set { _FirstName = value; }
        }

        private string _LastName;
        public string LastName
        {
            get { return _LastName; }
            set { _LastName = value; }
        }

        private bool _IsActive;
        public bool IsActive
        {
            get { return _IsActive; }
            set { _IsActive = value; }
        }
        // Methods

        public static DataTable GetAll()
        {
            DataTable users = new DataTable(TableName);

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["socialnexusConnectionString"].ConnectionString);
            con.Open();

            SqlCommand cmd = new SqlCommand("SELECT * FROM " + TableName, con);
            SqlDataReader reader = cmd.ExecuteReader();
            users.Load(reader);

            con.Close();

            return users;
        }

        public static AdminUser Authenticate(string username, string password)
        {
            AdminUser au = null;

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["socialnexusConnectionString"].ConnectionString);
            con.Open();

            SqlCommand cmd = new SqlCommand("SELECT TOP 1 AdminUserId, Username FROM " + 
                TableName + " WHERE Username = @USERNAME " +
                " AND Password = @PASSWORD", con);
            cmd.Parameters.Add(new SqlParameter("@USERNAME", username));
            cmd.Parameters.Add(new SqlParameter("@PASSWORD", password));

            SqlDataReader reader = cmd.ExecuteReader();

            if (reader.Read())
            {
                au = new AdminUser();
                au.AdminUserId = reader.GetInt32(0);
                au.Username = reader.GetString(1);
            }

            con.Close();
            return au;
        }

        public static AdminUser GetById(string id)
        {
            AdminUser su = null;

            if (String.IsNullOrEmpty(id))
                return su;

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["socialnexusConnectionString"].ConnectionString);
            con.Open();

            SqlCommand cmd = new SqlCommand("SELECT * FROM " +
                TableName + " WHERE AdminUserId = @ID", con);
            cmd.Parameters.Add(new SqlParameter("@ID", id));

            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                su = new AdminUser();
                su.AdminUserId = (int)reader["AdminUserId"];
                su.FirstName = reader["FirstName"].ToString().Trim();
                su.LastName = reader["LastName"].ToString().Trim();
                su.Username = reader["UserName"].ToString().Trim();
                su.IsActive = reader["IsActive"].ToString() == "True";
                su.Password = reader["Password"].ToString().Trim();
            }

            con.Close();

            return su;
        }

        public static bool UserNameExists(string username)
        {
            bool result = false;

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["socialnexusConnectionString"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM " + TableName +
                " WHERE Username = @USERNAME AND IsActive = 1", con);
            cmd.Parameters.Add(new SqlParameter("@USERNAME", username));

            SqlDataReader reader = cmd.ExecuteReader();
            result = reader.HasRows;

            con.Close();

            return result;
        }

        public void Update()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["socialnexusConnectionString"].ConnectionString);
            con.Open();

            SqlCommand cmd = new SqlCommand("UPDATE " + TableName +
                " SET FirstName = @FIRSTNAME, LastName = @LASTNAME, " +
                "IsActive = @ISACTIVE, Password = @PASSWORD, Username = @USERNAME" +
                " WHERE AdminUserId = @ID", con);
            cmd.Parameters.Add(new SqlParameter("@ID", AdminUserId));
            cmd.Parameters.Add(new SqlParameter("@FIRSTNAME", FirstName));
            cmd.Parameters.Add(new SqlParameter("@LASTNAME", LastName));
            cmd.Parameters.Add(new SqlParameter("@ISACTIVE", IsActive));
            cmd.Parameters.Add(new SqlParameter("@PASSWORD", Password));
            cmd.Parameters.Add(new SqlParameter("@USERNAME", Username));

            cmd.ExecuteNonQuery();

            con.Close();
        }

        public void Save()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["socialnexusConnectionString"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO " + TableName +
                " (Username, FirstName, LastName, IsActive, Password) VALUES " +
                "(@USERNAME, @FIRSTNAME, @LASTNAME, @ISACTIVE, @PASSWORD)", con);
            cmd.Parameters.Add(new SqlParameter("@USERNAME", Username));
            cmd.Parameters.Add(new SqlParameter("@FIRSTNAME", FirstName));
            cmd.Parameters.Add(new SqlParameter("@LASTNAME", LastName));
            cmd.Parameters.Add(new SqlParameter("@ISACTIVE", IsActive));
            cmd.Parameters.Add(new SqlParameter("@PASSWORD", Password));

            cmd.ExecuteNonQuery();

            con.Close();
        }

    }
}
